package com.csxy.yhj.service.impl;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.csxy.yhj.exception.MyException;
import com.csxy.yhj.service.VodService;
import com.csxy.yhj.util.ConstantVodUtils;
import com.csxy.yhj.util.InitObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideoAliyun(MultipartFile file) {
        //fileName：上传文件原始名称
        String fileName = file.getOriginalFilename();
        //title: 双穿之后显示名称
        String title = fileName.substring(0, fileName.lastIndexOf("."));
        //inputStream: 上传文件的输入流
        try {
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID
                    , ConstantVodUtils.ACCESS_KEY_SECRET
                    , title, fileName, inputStream);

            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);
            System.out.println("RequestId=" + response.getRequestId() + "\n");//请求视频点播服务的请求ID
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
                System.out.println("VideoId=" + getPlayUrl(videoId));
            } else {//如果设置回调，那么会返回如下响应
                videoId = response.getVideoId();
                System.out.println("VideoIde=" + getPlayUrl(videoId));
            }
            return videoId;

        } catch (IOException e) {
            throw new MyException(20001, "上传失败");
        }

    }
    //根据ID删除
    @Override
    public void removeAliyunVideoById(String id) {
        try{
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            DeleteVideoResponse response = client.getAcsResponse(request);
        }catch (ClientException e){
            throw new MyException(20001,"视频删除失败");
        }

    }


    //批量删除
    @Override
    public void removeMoreVideo(List<String> videoIdList) {
        //将集合转换为1，2，3格式
        String str = StringUtils.join(videoIdList.toArray(),",");
        try{
            //初始化对象
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除商品request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置要删除视频的id值
            request.setVideoIds(str);
            //调用初始化对象的方法实现删除
            DeleteVideoResponse response = client.getAcsResponse(request);
        }catch (Exception e){
            throw new MyException(20001,"视频删除失败");
        }
    }


    public String getPlayUrl(String videoId){
        try{
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET);
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(videoId);
            GetPlayInfoResponse response = client.getAcsResponse(request);
            return response.getPlayInfoList().get(0).getPlayURL();
        } catch (ClientException e) {
            throw new MyException(20001, "获取播放地址失败");
        }
    }
}
